#include"LinkQueue.h"

#include"LinkStack.h"

#include<string>
#include<iostream>


struct Car
{

    Car(){}
    Car(const std::string &name,int arriveTime):name(name),arriveTime(arriveTime){}
   
   
    std::string name; //车牌号

    int arriveTime;//到达时间
    int leaveTime;//离开时间


};
class CarStop
{
    public:
    CarStop(const int &size):size(size){}

    //进车
    void ComeCar();

    //出车
    void LeaveCar();


    void show();
    
    private:

        LinkStack stop;//停车场
        LinkQueue pavement;//便道



        const int size; //停车场可停数量
};




void CarStop::ComeCar()
{
    Car *c = new Car;
    std::cout<<"请输入车牌:";
    std::cin>>c->name;
    std::cout<<"请输入到达时间:";
    std::cin>>c->arriveTime;


    if(stop.Size() == size)
    {
        pavement.Push(c);
        std::cout<<"停车场位置已满,车牌："<<c->name<<"的车辆停在了便道上\n";
    }
    else
    {   stop.Push(c);
        std::cout<<"车牌"<<c->name<<"停在停车场内 "<<"到达时间"<<c->arriveTime<<std::endl;
    } 

}



void CarStop::LeaveCar()
{
    LinkStack temp;
    
    std::cout<<"请输入要离开的车牌:";
    std::string name;
    std::cin>>name;
    
    int leavetime = 0;
    while(!stop.IsEmpty())
    {
        Car *c = (Car*)*stop.GetTop();
        if(c->name == name)
        {
            std::cout<<"请输入离开的时间:";
            std::cin>>c->leaveTime;
            leavetime = c->leaveTime;

            int money = (c->leaveTime - c->arriveTime) * 1;
            std::cout<<"车辆"<<c->name<<"在"<<c->leaveTime<<"离开"<<"应该缴费："<<money<<std::endl;
            delete c;
            stop.Pop();
            break;
        }
        stop.Pop();
        temp.Push(c);

    }
    
    while(!temp.IsEmpty())
    {
        stop.Push(*temp.GetTop());
        temp.Pop();
    }

    //停车场不满 且便道上有车
    if(stop.Size() < size  && !pavement.IsEmpty())
    {
        Car *c = (Car*)*stop.GetTop();
        c->arriveTime = leavetime;
        std::cout<<"便道上的车"<<c->name<<"在"<< c->arriveTime<<"存进停车场"<<std::endl;
        pavement.Pop();
        stop.Push(c);
    }

}

void CarStop::show()
{
    LinkStack temp;
    std::cout<<"停车场:"<<std::endl;
    while(!stop.IsEmpty())
    {
        temp.Push(*stop.GetTop());
        stop.Pop();
    }
    
    while(!temp.IsEmpty())
    {
        Car *c = (Car*)*temp.GetTop();
        std::cout<<"车辆:"<<c->name<<"到达时间："<<c->arriveTime<<std::endl;
        stop.Push(c);
        temp.Pop();
    }

    std::cout<<"便道："<<std::endl;
    LinkQueue q;
    while(!q.IsEmpty())
    {
        q.Push(*pavement.GetFront());
        pavement.Pop();
    }
    while(!q.IsEmpty())
    {
        Car *c = (Car*)*q.GetFront();
        std::cout<<"车辆:"<<c->name<<std::endl;
        pavement.Push(c);
        q.Pop();
    }
}

int main()
{
    CarStop s(3);
        s.show();
        int choice = 0;
    // std::cout<<"1.进入停车场\n";
    // std::cout<<"2.离开停车场\n";
    // std::cout<<"3.退出程序\n";
while (1)
{ 
    std::cout<<"1.进入停车场\n";
    std::cout<<"2.离开停车场\n";
    std::cout<<"3.退出程序\n";


    std::cin>>choice;


    switch (choice)
    {

    case 1:s.ComeCar();
        break;
        
    case 2:s.LeaveCar();
        break;
    
    case 3:
        exit(-1);
    }

}

    return 0;
}